/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 *
 * @author omega
 */
public class CommonUtil {

    public static String URLEncode(String teks) {
        String hasil = "";
        for (int i = 0; i < teks.length(); i++) {
            if ((teks.charAt(i) >= '0' && teks.charAt(i) <= '9') || (teks.charAt(i) >= 'A' && teks.charAt(i) <= 'Z') || (teks.charAt(i) >= 'a' && teks.charAt(i) <= 'z')) {
                hasil = hasil + teks.charAt(i);
            } else {
                hasil = hasil + "%" + toHexString(teks.charAt(i));
            }
        }
        return hasil;
    }

    public static String toHexString(char c) {
        String hasil = "";
        int i = (int) c;
        int bil1 = i / 16;
        int bil2 = i % 16;
        if (bil1 < 10) {
            hasil = hasil + bil1;
        } else {
            hasil = hasil + (char) (55 + bil1);
        }
        if (bil2 < 10) {
            hasil = hasil + bil2;
        } else {
            hasil = hasil + (char) (55 + bil2);
        }
        return hasil;
    }

    public static void createGZip(String filename, String content) {
        GZIPOutputStream out = null;
        try {
            File f = new File(filename);
            f.createNewFile();
            out = new GZIPOutputStream(new FileOutputStream(filename));
            // Specify the input file to be compressed
            out.write(content.getBytes());
            // Finish creation of gzip file
            out.finish();
            out.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static InputStream readGzip(String pathFile) {
        InputStream in = null;
        try {
            in = new GZIPInputStream(new FileInputStream(new File(pathFile)));
        } catch (IOException ex) {
            Logger.getLogger(CommonUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return in;
    }

    public static InputStream readFile(String pathFile) {
        InputStream in = null;
        try {
            in = new FileInputStream(new File(pathFile));
        } catch (Exception e) {
        }
        return in;
    }

    public static String inputStreamToString(InputStream is) {
        String result = "";
        try {
            int ch = 0;
            byte[] buf = new byte[1024];
            StringBuffer sb = new StringBuffer();
            do {
                try {
                    ch = is.read(buf);
                } catch (Exception e) {
                    if (sb.length() == 0) {
                        System.gc();
                        throw e;
                    } else {
                        ch = -1;
                    }
                }
                if (ch != -1) {
                    System.gc();
                    sb.append(new String(buf, 0, ch, "ISO-8859-1"));
                }
            } while (ch != -1);
            result = sb.toString();
        } catch (Exception e) {
        }
        return result;
    }
}
